package com.jswdwsx.esign.commom

import com.jswdwsx.esign.cloud.response.WebResult
import org.slf4j.LoggerFactory
import java.lang.reflect.UndeclaredThrowableException

enum class ResEnum(private val code: String, private val msg: String) {

    Success("0", "success"),
    Exception("-1", "异常抛出！！"),
    Fail("99", "非指定性失败！"),
    ParamErr("1", "参数缺失或不合法！"),

    TemplateDoesNotExist("11", "签署流程模板不存在"),
    OrgDoesNotExist("13", "组织不存在"),
    UserDoesNotExist("12", "用户不存在"),
    FlowDoesNotExist("14", "签署流程不存在"),
    RequestDoesNotExist("15", "签署请求不存在"),
    FlowDoesInit("16", "签署流程不存在"),
    FileMustPdf("17", "模板文件必须为pdf"),
    CreateMerchantOrgFail("18", "创建商户组织失败"),
    SpecifySealSignNeedSealId("19", "指定印章的签署需要sealId"),
    SealDoNotExist("20", "印章不存在"),

    UserInfoRequired("21", "必须填写签约人信息"),
    OrgInfoRequired("22", "签约对象为组织时必须填写组织信息"),

    CallEsignUserCreateFail("31", "调用e签宝创建用户接口失败"),
    CallEsignOrgCreateFail("32", "调用e签宝创建组织接口失败"),
    CallEsignCreateTemplateFail("33", "调用e签宝创建文件模板接口失败"),
    CallEsignFlowCreateFail("34", "调用e签宝创建签署流程接口失败"),
    CallEsignFlowAddFileFail("35", "调用e签宝添加文件至签署流程接口失败"),
    CallEsignFlowStartFail("36", "调用e签宝开启签署流程接口失败"),
    CallEsignTokenFail("37", "获取e签宝token失败累积超过10次"),
    CallEsignUserUpdateFail("38", "调用e签宝修改用户接口失败"),
    CallEsignUserQueryFail("39", "调用e签宝查询用户接口失败"),
    CallEsignQueryTemplateFail("40", "调用e签宝查询文件模板接口失败"),
    CallEsignCreateSealImageFail("41", "调用e签宝创建图片印章接口失败"),
    CallEsignSetSilentSignFail("42", "调用e签宝设置静默签署接口失败"),
    CallEsignOrgSealCreateFail("43", "调用e签宝创建组织印章接口失败"),
    CallEsignPersonalSealCreateFail("44", "调用e签宝创建个人印章接口失败"),
    CallEsignOrgUpdateFail("45", "调用e签宝组织更新接口失败"),
    CallAddSignFieldFail("46", "调用e签宝添加签署区接口失败"),
    CallGetSignUrlFail("47", "调用e签宝获取签署url接口失败"),

    RequestAlreadyExist("51", "重复请求，签署请求已存在"),
    FlowAlreadyStart("52", "重复请求，流程已开启"),

    Base64ImageFail("61", "Base64ImageFail"),

    CallEsignApiRespError("71", "e签宝接口调用成功，但响应体为空"),
    CallEsignApiFail("72", "e签宝接口调用失败")

    ;

    fun isSuccess(): Boolean {
        return code == "0"
    }

    fun getCode(): String {
        return code
    }

    fun getMsg(): String {
        return msg
    }

    fun <T> parse(): WebResult<T> {
        return WebResult(code, msg)
    }

    fun <T> parse(data: T): WebResult<T> {
        return WebResult(code, msg, data)
    }

    fun <T> msg(msg: String): WebResult<T> {
        return WebResult(code, msg)
    }

    fun <T> msg(e: Throwable): WebResult<T> {
        var message = e.message
        if (e is UndeclaredThrowableException) {
            message = e.cause?.message
        }
        return WebResult(code, message)
    }


    companion object {
        private val logger = LoggerFactory.getLogger(ResEnum::class.java)

        fun <T> success(data: T): WebResult<T> {
            return Success.parse(data)
        }

        fun <T> success(): WebResult<T> {
            return Success.parse()
        }

        fun <T> fail(msg: String): WebResult<T> {
            logger.warn("请求失败：{}", msg)
            return Fail.msg(msg)
        }

        fun <T> exception(e: Throwable): WebResult<T> {
            return Exception.msg(e)
        }

        fun <T> fail(code: String, msg: String): WebResult<T> {
            logger.warn("请求失败：{}-{}", code, msg)
            return WebResult(code, msg)
        }
    }
}


